<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Casejilu extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['case_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($user_name2) && $user_name2 != ''){
                $where['user_username|user_phone'] = ['like', '%' . $user_name2 . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_like_case')->alias('l')
                ->join('up_case c','l.like_case_id=c.case_id','LEFT')
                ->join('up_u_user u','l.like_user_id=u.user_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $field = 'l.*,case_id,case_name,user_id,user_username,user_phone,user_d_name,user_image';
            $lists = Db::table('up_like_case')->alias('l')
                ->join('up_case c','l.like_case_id=c.case_id','LEFT')
                ->join('up_u_user u','l.like_user_id=u.user_id','LEFT')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('like_jilu_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    if($v['user_d_name']) $lists[$k]['user_username'] = "{$v['user_username']}({$v['user_d_name']})";
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 案例订单
     */
    public function caseOrder(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'jilu_user_id' => $user_id,
                'jilu_case_id' => $case_id
            ];
            if(isset($user_name) && $user_name != ''){
                $where['jilu_content'] = ['like', '%' . $user_name . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_like_case_shop_jilu')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_like_case_shop_jilu')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('jilu_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $case_id = input('case_id');
        $user_id = input('user_id');
        $this->assign('case_id',$case_id);
        $this->assign('user_id',$user_id);
        return $this->fetch('casejilu/order');
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function orderDel(){
        $nt_id = input('id');
        $res = Db::table('up_like_case_shop_jilu')->where('jilu_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}